﻿@model zichan.Entity.Role
@{
    ViewBag.Title = "角色编辑";
}

@section header{
    <style type="text/css">
        .tree .tree-item .icon-file {
            width: 14px;
        }

        .tree .tree-item {
            height: auto;
            padding: 0px;
        }

            .tree .tree-item:hover {
                background-color: white;
            }

        .tree .tree-item, .tree .tree-folder {
            border: 0px solid #FFF;
        }

        .tree a {
            text-decoration: none;
            display: inline-block !important;
        }

        .tree .tool {
            padding-left: 10px;
            visibility: hidden;
        }

        .tree .le {
            padding: 0px 10px;
            line-height: 35px;
        }

        .le:hover {
            background-color: #f1f1f1;
        }

            .le:hover .tool {
                visibility: visible;
            }

        .level0 .til {
            font-weight: bold;
        }



        .checkbox {
            padding: 0 !important;
            margin: 0 !important;
            display: inline;
            height: 35px;
            vertical-align: top;
        }

        div.divider {
            padding: 0;
        }
    </style>
}

@helper ShowSelected(int mid)
{
List<int> Ids = Model.Powers.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(o => Convert.ToInt32(o)).ToList();
if (Ids.Contains(mid))
{
        @:checked
   }
}


@helper MeunItem(zichan.Entity.Menu m, bool InChild, bool open)
{
    <div class="level@(m.Level) le clearfix" id="nut(@m.Id)">
        <i class="pull-left @(InChild ? "icon blue fa " + (open ? "fa-folder-open" : "fa-folder") : " fa fa-file blue")" style=" margin-top:10px;"></i>
        <input name="Powers" type="checkbox" value="@m.Id" @ShowSelected(m.Id) class="checkbox pull-left" style="padding-left: 2px; padding-right:2px;">
        <span class="til pull-left">@m.Title</span>
        @if (InChild)
        {
            <span class="selall  til  pull-left" style="color:#ccc; padding-left:5px">全选</span>
        }
    </div>
}


@section Pagetitle{
    @AdminCommon.PageTitle("角色编辑", "修改数据")
}


@using (Html.BeginForm("edit", "Role", FormMethod.Post, new { @class = "form-horizontal" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(o => o.Id)
    <div class="form-group">
        <label for="Title" class="col-sm-2 control-label no-padding-right">角色名称:</label>
        <div class="col-sm-9">
            @Html.TextBoxFor(o => o.Name, new { @class = "form-control input-large" })
            @Html.ValidationMessageFor(o => o.Name)
        </div>
    </div>

    <div class="form-group">
        <label for="Title" class="col-sm-2 control-label no-padding-right">权限:</label>
        <div class="col-sm-9">

            <div class="tree">
                @{
                    List<zichan.Entity.Menu> items = ((List<zichan.Entity.Menu>)ViewBag.Menus);
                    var topms1 = items.Where(o => o.Level == 0).OrderBy(o => o.Sort);
                    foreach (var tm in topms1)
                    {
                        var topms2 = items.Where(o => o.Path.Contains("," + tm.Id + ",") && o.Level == 1).OrderBy(o => o.Sort).ToList();
                        bool istm = topms2.Count() > 0;
                        <div class="tree-item">
                            @MeunItem(tm, istm, true)
                            @if (istm)
                            {
                                <div class="tree" style="margin-left: 25px;">
                                    @foreach (var tm2 in topms2)
                                    {
                                        var topms3 = items.Where(o => o.Path.Contains("," + tm2.Id + ",") && o.Level == 2).OrderBy(o => o.Sort).ToList();
                                        bool istm2 = topms3.Count() > 0;
                                        <div class="tree-item">
                                            @MeunItem(tm2, istm2, true)
                                            @if (istm2)
                            {
                                                <div class="tree " style="margin-left: 25px;">
                                                    @foreach (var tm3 in topms3)
                                                    {
                                                        <div class="tree-item">@MeunItem(tm3, false, false)</div>
                                                    }
                                                </div>
                                            }
                                        </div>
                                    }
                                </div>
                            }
                        </div>
                    }
                }
            </div>

        </div>
    </div>




    <div class="clearfix form-actions">
        <label class="col-sm-2 control-label"></label>
        <div class=" col-md-9">
            <button type="submit" class="btn btn-info" id="btnSubmit">
                <i class="fa fa-arrow-right bigger-110"></i>
                提交
            </button>
            <button class="btn" type="reset">
                <i class="icon-undo bigger-110"></i>
                清空
            </button>
        </div>
    </div>
                    }


@section footer{
@Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
        $(function () {
            $(".icon").on("click", function () {
                $(this).toggleClass("fa-folder-open").toggleClass("fa-folder").parent().next("div").toggleClass("hide");
            })

            $(".checkbox").on("change", function () {
                var b = $(this).prop("checked");
                // $(".checkbox", $(this).parent().next("ul")).prop("checked", b);
                if (b) {
                    $(".checkbox", $(this).parent().parent().parent().prev("div")).prop("checked", b);
                    $(".checkbox", $(this).parent().parent().parent().parent().parent().prev("div")).prop("checked", b);
                }
                else {
                    $(".checkbox", $(this).parent().parent()).prop("checked", false);
                }
            })

            $(".selall").on("click", function () {

                var b = $(this).attr("data-c");
                if (b == "t") {
                    $(".checkbox", $(this).parent().parent()).prop("checked", false);
                    $(this).attr("data-c", "f");
                }
                else {
                    $(".checkbox", $(this).parent().parent()).prop("checked", true);
                    $(this).attr("data-c", "t");
                }
            })
        });
    </script>
}



